<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>classlist.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_dom_classlist.js.html">classlist.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2012 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Utilities for detecting, adding and removing classes.  Prefer
<a name="line17"></a> * this over goog.dom.classes for new code since it attempts to use classList
<a name="line18"></a> * (DOMTokenList: http://dom.spec.whatwg.org/#domtokenlist) which is faster
<a name="line19"></a> * and requires less code.
<a name="line20"></a> *
<a name="line21"></a> * Note: these utilities are meant to operate on HTMLElements and
<a name="line22"></a> * will not work on elements with differing interfaces (such as SVGElements).
<a name="line23"></a> */
<a name="line24"></a>
<a name="line25"></a>
<a name="line26"></a>goog.provide(&#39;goog.dom.classlist&#39;);
<a name="line27"></a>
<a name="line28"></a>goog.require(&#39;goog.array&#39;);
<a name="line29"></a>goog.require(&#39;goog.asserts&#39;);
<a name="line30"></a>
<a name="line31"></a>
<a name="line32"></a>/**
<a name="line33"></a> * Override this define at build-time if you know your target supports it.
<a name="line34"></a> * @define {boolean} Whether to use the classList property (DOMTokenList).
<a name="line35"></a> */
<a name="line36"></a>goog.dom.classlist.ALWAYS_USE_DOM_TOKEN_LIST = false;
<a name="line37"></a>
<a name="line38"></a>
<a name="line39"></a>/**
<a name="line40"></a> * Enables use of the native DOMTokenList methods.  See the spec at
<a name="line41"></a> * {@link http://dom.spec.whatwg.org/#domtokenlist}.
<a name="line42"></a> * @type {boolean}
<a name="line43"></a> * @private
<a name="line44"></a> */
<a name="line45"></a>goog.dom.classlist.NATIVE_DOM_TOKEN_LIST_ =
<a name="line46"></a>    goog.dom.classlist.ALWAYS_USE_DOM_TOKEN_LIST ||
<a name="line47"></a>    typeof DOMTokenList != &#39;undefined&#39;;
<a name="line48"></a>
<a name="line49"></a>
<a name="line50"></a>/**
<a name="line51"></a> * Gets an array-like object of class names on an element.
<a name="line52"></a> * @param {Element} element DOM node to get the classes of.
<a name="line53"></a> * @return {!goog.array.ArrayLike} Class names on {@code element}.
<a name="line54"></a> */
<a name="line55"></a>goog.dom.classlist.get = goog.dom.classlist.NATIVE_DOM_TOKEN_LIST_ ?
<a name="line56"></a>    function(element) {
<a name="line57"></a>      return element.classList;
<a name="line58"></a>    } :
<a name="line59"></a>    function(element) {
<a name="line60"></a>      var className = element.className;
<a name="line61"></a>      // Some types of elements don&#39;t have a className in IE (e.g. iframes).
<a name="line62"></a>      // Furthermore, in Firefox, className is not a string when the element is
<a name="line63"></a>      // an SVG element.
<a name="line64"></a>      return goog.isString(className) &amp;&amp; className.match(/\S+/g) || [];
<a name="line65"></a>    };
<a name="line66"></a>
<a name="line67"></a>
<a name="line68"></a>/**
<a name="line69"></a> * Sets the entire class name of an element.
<a name="line70"></a> * @param {Element} element DOM node to set class of.
<a name="line71"></a> * @param {string} className Class name(s) to apply to element.
<a name="line72"></a> */
<a name="line73"></a>goog.dom.classlist.set = function(element, className) {
<a name="line74"></a>  element.className = className;
<a name="line75"></a>};
<a name="line76"></a>
<a name="line77"></a>
<a name="line78"></a>/**
<a name="line79"></a> * Returns true if an element has a class.  This method may throw a DOM
<a name="line80"></a> * exception for an invalid or empty class name if DOMTokenList is used.
<a name="line81"></a> * @param {Element} element DOM node to test.
<a name="line82"></a> * @param {string} className Class name to test for.
<a name="line83"></a> * @return {boolean} Whether element has the class.
<a name="line84"></a> */
<a name="line85"></a>goog.dom.classlist.contains = goog.dom.classlist.NATIVE_DOM_TOKEN_LIST_ ?
<a name="line86"></a>    function(element, className) {
<a name="line87"></a>      goog.asserts.assert(!!element.classList);
<a name="line88"></a>      return element.classList.contains(className);
<a name="line89"></a>    } :
<a name="line90"></a>    function(element, className) {
<a name="line91"></a>      return goog.array.contains(goog.dom.classlist.get(element), className);
<a name="line92"></a>    };
<a name="line93"></a>
<a name="line94"></a>
<a name="line95"></a>/**
<a name="line96"></a> * Adds a class to an element.  Does not add multiples of class names.  This
<a name="line97"></a> * method may throw a DOM exception for an invalid or empty class name if
<a name="line98"></a> * DOMTokenList is used.
<a name="line99"></a> * @param {Element} element DOM node to add class to.
<a name="line100"></a> * @param {string} className Class name to add.
<a name="line101"></a> */
<a name="line102"></a>goog.dom.classlist.add = goog.dom.classlist.NATIVE_DOM_TOKEN_LIST_ ?
<a name="line103"></a>    function(element, className) {
<a name="line104"></a>      element.classList.add(className);
<a name="line105"></a>    } :
<a name="line106"></a>    function(element, className) {
<a name="line107"></a>      if (!goog.dom.classlist.contains(element, className)) {
<a name="line108"></a>        // Ensure we add a space if this is not the first class name added.
<a name="line109"></a>        element.className += element.className.length &gt; 0 ?
<a name="line110"></a>            (&#39; &#39; + className) : className;
<a name="line111"></a>      }
<a name="line112"></a>    };
<a name="line113"></a>
<a name="line114"></a>
<a name="line115"></a>/**
<a name="line116"></a> * Convenience method to add a number of class names at once.
<a name="line117"></a> * @param {Element} element The element to which to add classes.
<a name="line118"></a> * @param {goog.array.ArrayLike.&lt;string&gt;} classesToAdd An array-like object
<a name="line119"></a> * containing a collection of class names to add to the element.
<a name="line120"></a> * This method may throw a DOM exception if classesToAdd contains invalid
<a name="line121"></a> * or empty class names.
<a name="line122"></a> */
<a name="line123"></a>goog.dom.classlist.addAll = goog.dom.classlist.NATIVE_DOM_TOKEN_LIST_ ?
<a name="line124"></a>    function(element, classesToAdd) {
<a name="line125"></a>      goog.array.forEach(classesToAdd, function(className) {
<a name="line126"></a>        goog.dom.classlist.add(element, className);
<a name="line127"></a>      });
<a name="line128"></a>    } :
<a name="line129"></a>    function(element, classesToAdd) {
<a name="line130"></a>      var classMap = {};
<a name="line131"></a>
<a name="line132"></a>      // Get all current class names into a map.
<a name="line133"></a>      goog.array.forEach(goog.dom.classlist.get(element),
<a name="line134"></a>          function(className) {
<a name="line135"></a>            classMap[className] = true;
<a name="line136"></a>          });
<a name="line137"></a>
<a name="line138"></a>      // Add new class names to the map.
<a name="line139"></a>      goog.array.forEach(classesToAdd,
<a name="line140"></a>          function(className) {
<a name="line141"></a>            classMap[className] = true;
<a name="line142"></a>          });
<a name="line143"></a>
<a name="line144"></a>      // Flatten the keys of the map into the className.
<a name="line145"></a>      element.className = &#39;&#39;;
<a name="line146"></a>      for (var className in classMap) {
<a name="line147"></a>        element.className += element.className.length &gt; 0 ?
<a name="line148"></a>            (&#39; &#39; + className) : className;
<a name="line149"></a>      }
<a name="line150"></a>    };
<a name="line151"></a>
<a name="line152"></a>
<a name="line153"></a>/**
<a name="line154"></a> * Removes a class from an element.  This method may throw a DOM exception
<a name="line155"></a> * for an invalid or empty class name if DOMTokenList is used.
<a name="line156"></a> * @param {Element} element DOM node to remove class from.
<a name="line157"></a> * @param {string} className Class name to remove.
<a name="line158"></a> */
<a name="line159"></a>goog.dom.classlist.remove = goog.dom.classlist.NATIVE_DOM_TOKEN_LIST_ ?
<a name="line160"></a>    function(element, className) {
<a name="line161"></a>      element.classList.remove(className);
<a name="line162"></a>    } :
<a name="line163"></a>    function(element, className) {
<a name="line164"></a>      if (goog.dom.classlist.contains(element, className)) {
<a name="line165"></a>        // Filter out the class name.
<a name="line166"></a>        element.className = goog.array.filter(
<a name="line167"></a>            goog.dom.classlist.get(element),
<a name="line168"></a>            function(c) {
<a name="line169"></a>              return c != className;
<a name="line170"></a>            }).join(&#39; &#39;);
<a name="line171"></a>      }
<a name="line172"></a>    };
<a name="line173"></a>
<a name="line174"></a>
<a name="line175"></a>/**
<a name="line176"></a> * Removes a set of classes from an element.  Prefer this call to
<a name="line177"></a> * repeatedly calling {@code goog.dom.classlist.remove} if you want to remove
<a name="line178"></a> * a large set of class names at once.
<a name="line179"></a> * @param {Element} element The element from which to remove classes.
<a name="line180"></a> * @param {goog.array.ArrayLike.&lt;string&gt;} classesToRemove An array-like object
<a name="line181"></a> * containing a collection of class names to remove from the element.
<a name="line182"></a> * This method may throw a DOM exception if classesToRemove contains invalid
<a name="line183"></a> * or empty class names.
<a name="line184"></a> */
<a name="line185"></a>goog.dom.classlist.removeAll = goog.dom.classlist.NATIVE_DOM_TOKEN_LIST_ ?
<a name="line186"></a>    function(element, classesToRemove) {
<a name="line187"></a>      goog.array.forEach(classesToRemove, function(className) {
<a name="line188"></a>        goog.dom.classlist.remove(element, className);
<a name="line189"></a>      });
<a name="line190"></a>    } :
<a name="line191"></a>    function(element, classesToRemove) {
<a name="line192"></a>      // Filter out those classes in classesToRemove.
<a name="line193"></a>      element.className = goog.array.filter(
<a name="line194"></a>          goog.dom.classlist.get(element),
<a name="line195"></a>          function(className) {
<a name="line196"></a>            // If this class is not one we are trying to remove,
<a name="line197"></a>            // add it to the array of new class names.
<a name="line198"></a>            return !goog.array.contains(classesToRemove, className);
<a name="line199"></a>          }).join(&#39; &#39;);
<a name="line200"></a>    };
<a name="line201"></a>
<a name="line202"></a>
<a name="line203"></a>/**
<a name="line204"></a> * Adds or removes a class depending on the enabled argument.  This method
<a name="line205"></a> * may throw a DOM exception for an invalid or empty class name if DOMTokenList
<a name="line206"></a> * is used.
<a name="line207"></a> * @param {Element} element DOM node to add or remove the class on.
<a name="line208"></a> * @param {string} className Class name to add or remove.
<a name="line209"></a> * @param {boolean} enabled Whether to add or remove the class (true adds,
<a name="line210"></a> *     false removes).
<a name="line211"></a> */
<a name="line212"></a>goog.dom.classlist.enable = function(element, className, enabled) {
<a name="line213"></a>  if (enabled) {
<a name="line214"></a>    goog.dom.classlist.add(element, className);
<a name="line215"></a>  } else {
<a name="line216"></a>    goog.dom.classlist.remove(element, className);
<a name="line217"></a>  }
<a name="line218"></a>};
<a name="line219"></a>
<a name="line220"></a>
<a name="line221"></a>/**
<a name="line222"></a> * Switches a class on an element from one to another without disturbing other
<a name="line223"></a> * classes. If the fromClass isn&#39;t removed, the toClass won&#39;t be added.  This
<a name="line224"></a> * method may throw a DOM exception if the class names are empty or invalid.
<a name="line225"></a> * @param {Element} element DOM node to swap classes on.
<a name="line226"></a> * @param {string} fromClass Class to remove.
<a name="line227"></a> * @param {string} toClass Class to add.
<a name="line228"></a> * @return {boolean} Whether classes were switched.
<a name="line229"></a> */
<a name="line230"></a>goog.dom.classlist.swap = function(element, fromClass, toClass) {
<a name="line231"></a>  if (goog.dom.classlist.contains(element, fromClass)) {
<a name="line232"></a>    goog.dom.classlist.remove(element, fromClass);
<a name="line233"></a>    goog.dom.classlist.add(element, toClass);
<a name="line234"></a>    return true;
<a name="line235"></a>  }
<a name="line236"></a>  return false;
<a name="line237"></a>};
<a name="line238"></a>
<a name="line239"></a>
<a name="line240"></a>/**
<a name="line241"></a> * Removes a class if an element has it, and adds it the element doesn&#39;t have
<a name="line242"></a> * it.  Won&#39;t affect other classes on the node.  This method may throw a DOM
<a name="line243"></a> * exception if the class name is empty or invalid.
<a name="line244"></a> * @param {Element} element DOM node to toggle class on.
<a name="line245"></a> * @param {string} className Class to toggle.
<a name="line246"></a> * @return {boolean} True if class was added, false if it was removed
<a name="line247"></a> *     (in other words, whether element has the class after this function has
<a name="line248"></a> *     been called).
<a name="line249"></a> */
<a name="line250"></a>goog.dom.classlist.toggle = function(element, className) {
<a name="line251"></a>  var add = !goog.dom.classlist.contains(element, className);
<a name="line252"></a>  goog.dom.classlist.enable(element, className, add);
<a name="line253"></a>  return add;
<a name="line254"></a>};
<a name="line255"></a>
<a name="line256"></a>
<a name="line257"></a>/**
<a name="line258"></a> * Adds and removes a class of an element.  Unlike
<a name="line259"></a> * {@link goog.dom.classlist.swap}, this method adds the classToAdd regardless
<a name="line260"></a> * of whether the classToRemove was present and had been removed.  This method
<a name="line261"></a> * may throw a DOM exception if the class names are empty or invalid.
<a name="line262"></a> *
<a name="line263"></a> * @param {Element} element DOM node to swap classes on.
<a name="line264"></a> * @param {string} classToRemove Class to remove.
<a name="line265"></a> * @param {string} classToAdd Class to add.
<a name="line266"></a> */
<a name="line267"></a>goog.dom.classlist.addRemove = function(element, classToRemove, classToAdd) {
<a name="line268"></a>  goog.dom.classlist.remove(element, classToRemove);
<a name="line269"></a>  goog.dom.classlist.add(element, classToAdd);
<a name="line270"></a>};
</pre>


</body>
</html>
